#include <algorithm>
#include <map>
#include "AtomString.h"

using std::string;
using std::vector;
using std::find;
using std::map;
using std::pair;

class ProxyString
{
public:
	ProxyString(const std::string &s) : m_string(s)
	{
	}
	ProxyString &operator = (const ProxyString &rhs)
	{
		m_string = rhs.m_string;
		return *this;
	}
	bool operator == (const ProxyString &rhs) const
	{
		return this == &rhs;
	}
	bool operator == (const std::string &s) const
	{
		return m_string == s;
	}
protected:
	std::string m_string;
};

vector<ProxyString> AtomString::s_vecProxyString;
map<string, size_t> g_mapString;
map<string, size_t>::iterator g_LastIt = g_mapString.end();

size_t AtomString::_Find(const string &s)
{
	map<string, size_t>::iterator it = g_mapString.find(s);
	if (it == g_mapString.end())
	{
		g_LastIt = it;
		return size_t(-1);
	}

	return it->second;
	/*vector<ProxyString>::iterator it = std::find(s_vecProxyString.begin(), s_vecProxyString.end(), s);
	if (it == s_vecProxyString.end())
	{
		return size_t(-1);
	}
	return it - s_vecProxyString.begin();
	*/
}

size_t AtomString::_Add(const string &s)
{
	size_t nIndex = s_vecProxyString.size();
	s_vecProxyString.push_back(ProxyString(s));
	g_mapString.insert(g_LastIt, pair<string, size_t>(s, nIndex));
	return nIndex;
}
